package ws

import (
	"net/http"

	"gitee.com/crack007/goose/gslog"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
)

var upGrader = websocket.Upgrader{
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

func RegisterRoute(wsServer *Server, path string, handle ISessionHandle) {
	wsServer.Engine.GET(path, func(ctx *gin.Context) {
		conn, err := upGrader.Upgrade(ctx.Writer, ctx.Request, nil)
		if err != nil {
			gslog.Error(err)
			return
		}

		wsSession := wsServer.SessionBuilder.build(conn, handle, ctx)
		wsSession.Server = wsServer
		wsSession.Process()
	})
}
